package com.kws.system.service.merchant.impl;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kws.common.security.utils.SecurityUtils;
import com.kws.system.domain.info.QueryMerchantUserInfo;
import com.kws.system.domain.merchant.MerchantExamineRq;
import com.kws.system.domain.merchant.QueryDeviceAuditRq;
import com.kws.system.domain.merchant.TbDevice;
import com.kws.system.domain.merchant.TbDeviceAudit;
import com.kws.system.domain.merchant.info.QueryDeviceAuditInfo;
import com.kws.system.domain.merchant.info.QueryDeviceInfo;
import com.kws.system.domain.vo.CreateProductListingReviewMessageRq;
import com.kws.system.mapper.merchant.DeviceAuditMapper;
import com.kws.system.service.merchant.DeviceAuditService;
import com.kws.system.service.merchant.TbDeviceAuditService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <b>DeviceAuditServiceImpl</b>
 *
 * @description: DeviceAuditServiceImpl <br>
 * @date: 2024/12/5 14:05 <br>
 * @author: lhm <br>
 * @version: 1.0 <br>
 */

@Service
public class DeviceAuditServiceImpl extends ServiceImpl<DeviceAuditMapper, TbDevice> implements DeviceAuditService {
    private static final Logger logger = LoggerFactory.getLogger(DeviceAuditServiceImpl.class);

    @Autowired
    private TbDeviceAuditService tbDeviceAuditService;

    @Override
    public Page<QueryDeviceAuditInfo> queryDeviceAuditPage(QueryDeviceAuditRq rq) {
        Page<QueryDeviceAuditInfo> page = new Page<>(rq.getPageNum(), rq.getPageSize());
        Page<QueryDeviceAuditInfo> pageInfo = baseMapper.queryDeviceAuditPage(page,rq);
        return pageInfo;
    }

    @Override
    public QueryDeviceInfo queryDeviceAuditById(Long id) {
        return baseMapper.queryDeviceAuditById(id);
    }

    @Override
    public boolean deleteDeviceById(Long id) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id",id);
        updateWrapper.set("enable","0");
        updateWrapper.set("update_time",new Date());
        int update = baseMapper.update(null, updateWrapper);
        if (1 == update){
            return true;
        }
        return false;
    }

    @Override
    public boolean deviceAuditFirstInstance(MerchantExamineRq rq) {
        TbDeviceAudit tbDeviceAudit = new TbDeviceAudit();
        tbDeviceAudit.setDeviceId(rq.getMerchantId());
        tbDeviceAudit.setExamineUser(SecurityUtils.getStringUserId());
        tbDeviceAudit.setExamineStatus(rq.getExamineStatus());
        tbDeviceAudit.setExamineTime(new Date());
        tbDeviceAudit.setExamineHierarchy("0");
        tbDeviceAudit.setRemark(rq.getRemark());
        if ("0".equals(rq.getExamineStatus())){
            UpdateWrapper updateWrapper = new UpdateWrapper();
            updateWrapper.eq("id",rq.getMerchantId());
            updateWrapper.set("audit_status","3");
            baseMapper.update(null,updateWrapper);
        }else if ("1".equals(rq.getExamineStatus())){
            UpdateWrapper updateWrapper = new UpdateWrapper();
            updateWrapper.eq("id",rq.getMerchantId());
            updateWrapper.set("audit_status","1");
            baseMapper.update(null,updateWrapper);
        }
        boolean save = tbDeviceAuditService.save(tbDeviceAudit);

        return save;
    }

    @Override
    public boolean deviceAuditSecondInstance(MerchantExamineRq rq) {
        TbDeviceAudit tbDeviceAudit = new TbDeviceAudit();
        tbDeviceAudit.setDeviceId(rq.getMerchantId());
        tbDeviceAudit.setExamineUser(SecurityUtils.getStringUserId());
        tbDeviceAudit.setExamineStatus(rq.getExamineStatus());
        tbDeviceAudit.setExamineTime(new Date());
        tbDeviceAudit.setExamineHierarchy("0");
        tbDeviceAudit.setRemark(rq.getRemark());
        if ("0".equals(rq.getExamineStatus())){
            UpdateWrapper updateWrapper = new UpdateWrapper();
            updateWrapper.eq("id",rq.getMerchantId());
            updateWrapper.set("audit_status","3");
            baseMapper.update(null,updateWrapper);
        }else if ("1".equals(rq.getExamineStatus())){
            UpdateWrapper updateWrapper = new UpdateWrapper();
            updateWrapper.eq("id",rq.getMerchantId());
            updateWrapper.set("audit_status","2");
            baseMapper.update(null,updateWrapper);
        }
        boolean save = tbDeviceAuditService.save(tbDeviceAudit);
        if (save && "1".equals(rq.getExamineStatus())){
            QueryMerchantUserInfo info = baseMapper.queryMerchantUserByDeviceId(rq.getMerchantId());
            CreateProductListingReviewMessageRq reviewMessageRq = new CreateProductListingReviewMessageRq();
            reviewMessageRq.setSendUserId(info.getUserId());
            reviewMessageRq.setMessageTemplateType("productListingApproved");
            reviewMessageRq.setMerchantName(info.getCompanyName());
            reviewMessageRq.setPlatformName("千瓦时微电网平台");
            reviewMessageRq.setNotificationTime(DateUtil.format(new Date(), DatePattern.NORM_DATETIME_FORMATTER));
        }
        return save;
    }
}
